package com.gaohe.controller;


import com.gaohe.common.R;
import com.gaohe.pojo.StadBuyCard;
import com.gaohe.pojo.StadCourse;
import com.gaohe.pojo.StadCourseRecord;
import com.gaohe.service.StadBuyCardService;
import com.gaohe.service.StadCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author liu
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/course")
public class StadCourseController {
    @Autowired
    private StadBuyCardService stadBuyCardService;
    @Autowired
    private StadCourseService stadCourseService;
//    获得总旷课记录
    @GetMapping("/record/{id}")
    public R getCourseRecord(@PathVariable("id")Long id){
        final List<StadCourseRecord> allCourseRecord = stadCourseService.getAllCourseRecord(id);
        System.err.println(allCourseRecord);
        final long record = allCourseRecord.stream().filter(req -> req.getStatus().equals("旷课")).count();
        return R.success(record);
    }
//获得这个月的旷课总数
    @GetMapping("recordOnMonth/{id}")
    public R getCourseOnMonth(@PathVariable("id")Long id){
        final List<StadCourseRecord> allCourseRecord = stadCourseService.getAllCourseRecord(id);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date();
        Integer month=date.getMonth()+1;
        final long count = allCourseRecord.stream()
                .filter(req -> Integer.valueOf(req.getSignTime().format(formatter).split("")[6]) == month)
                .filter(req -> req.getStatus().equals("旷课"))
                .count();
        return R.success(count);
    }
//   获得会员的团课总数
    @GetMapping("getAllTuanKe/{id}")
    public R getAllTuanKe(@PathVariable("id") Long id){
        final List<StadBuyCard> buyCardById = stadBuyCardService.getBuyCardById(id);
        final List<StadCourse> courseList = stadCourseService.getCourseList();
        if (buyCardById.size()!=0){
            return R.success(courseList.stream().filter(req->req.getType().equals("团课")).count());
        }
        return R.success(0);
    }
    @GetMapping("getAllTuanKeOnMonth/{id}")
    public R getAllTuanKeOnMonth(@PathVariable("id")Long id){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date();
        Integer month=date.getMonth()+1;
        final List<StadBuyCard> buyCardById = stadBuyCardService.getBuyCardById(id);
        System.err.println("card:"+buyCardById);
        final List<StadCourse> courseList = stadCourseService.getCourseList();
        if (buyCardById.size()!=0){
            return R.success(courseList.stream()
                    .filter(req -> Integer.valueOf(req.getEndTime().format(formatter).split("")[6]) == month)
                    .filter(req->req.getType().equals("团课")).count());
        }
        System.err.println("yundao");
        return R.success(0);
    }
    @GetMapping("getAllSiJIao/{id}")
    public R getAllSijiao(@PathVariable("id") Long id){
        final List<StadBuyCard> buyCardById = stadBuyCardService.getBuyCardById(id);
        final List<StadCourse> courseList = stadCourseService.getCourseList();
        if (buyCardById.size()!=0){
            return R.success(courseList.stream().filter(req->req.getType().equals("私教")).count());
        }
        return R.success(0);
    }
    @GetMapping("getAllSiJiaoOnMonth/{id}")
    public R getAllSijiaoOnMonth(@PathVariable("id")Long id){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date();
        Integer month=date.getMonth()+1;
        List<StadBuyCard> buyCardById = stadBuyCardService.getBuyCardById(id);
        List<StadCourse> courseList = stadCourseService.getCourseList();
        if (buyCardById.size()!=0){
            return R.success(courseList.stream()
                    .filter(req -> Integer.valueOf(req.getEndTime().format(formatter).split("")[6]) == month)
                    .filter(req->req.getType().equals("私教")).count());
        }
        return R.success(0);
    }
//    获得课程信息列表
    @GetMapping("getCourseAndDetailList/{id}")
    public R getCourseAndDetailList(@PathVariable("id")Long id){
        final List<StadBuyCard> buyCardList = stadBuyCardService.getBuyCardList(id);
        final List<StadCourse> courseList = stadCourseService.getCourseAndDetailsList(id);
        if (buyCardList.size()==0){
            return R.success(0);
        }
        return R.success(courseList);
    }
}

